import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        System.out.println("请输入指定目录：");
        Scanner scan = new Scanner(System.in);
        String str = scan.next();

        File file = new File(str);
        if(!file.isDirectory()) {
            System.out.println("您输入的不是目录或者该目录不存在...");
            return;
        }

        System.out.println("请输入指定的字符：");
        String token = scan.next();

        List<File> list = new ArrayList<>();
        scanFile(file, token);
    }

    private static void scanFile(File file, String token) {
        File[] ret = file.listFiles();
        if(ret == null) {
            return;
        }
        for (int i = 0; i < ret.length; i++) {
            if(ret[i].isDirectory()) {
                scanFile(ret[i], token);
            } else {
                delFile(ret[i], token);
            }
        }
    }

    private static void delFile(File file, String token) {
        if(file.getName().contains(token)) {
            System.out.println("该文件包含你指定的字符内容：" + file.getAbsoluteFile());
            System.out.println("是否进行删除：Y / N");
            Scanner scan = new Scanner(System.in);
            String ret = scan.next();
            if(ret.equals("Y")) {
                file.delete();
            }
        }
    }
}
